/**   
 * @Title: LoginTimeControl.java 
 * @Package: com.esun.util 
 * @Description: TODO(登录作用时间工具类) 
 * @Author: Billy qigs@500wan.com   
 * @Date: 2011-7-5 上午10:55:17  
 * Copyright (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.util;

import android.app.Dialog;
import android.content.Context;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.esun.config.Config;
import com.esun.model.UserInfo;
import com.esun.ui.R;
import com.esun.util.log.EDebug;
import com.esun.util.md5.MD5;

/**
 * @ClassName: LoginTimeControl
 * @Description: TODO(登录作用时间工具类)
 * @Author: Billy qigs@500wan.com
 * @Date: 2011-7-5 上午10:55:17
 */
public class LoginTimeControl {

	public boolean isNeedInputPwd() {
		long durationTime = System.currentTimeMillis() - UserInfo.getUserInfo().getInputPasswordTime();

		if (UserInfo.getUserInfo().getAccountProtectState()) {
			if (durationTime > Config.NEEDINPUTPWD_TIME) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

	public void showInputPasswordDialog(final Context context) {
		final Dialog inputPwdDialog = new Dialog(context, R.style.dialog);
		inputPwdDialog.getWindow().setContentView(R.layout.dialog_edittext_continuebuyl);
		Button btnYes = (Button) inputPwdDialog.getWindow().findViewById(R.id.dialog_continue_btn_yes);
		((TextView) inputPwdDialog.getWindow().findViewById(R.id.dialog_edittext_tv_infoBefore)).setText("");
		((TextView) inputPwdDialog.getWindow().findViewById(R.id.dialog_edittext_tv_infoBehind)).setText("");
		((TextView) inputPwdDialog.getWindow().findViewById(R.id.dialog_deittext_tv_titleTop)).setText("请输入您的500WAN账户密码");
		((TextView) inputPwdDialog.getWindow().findViewById(R.id.dialog_deittext_tv_title)).setText("用户名：" + UserInfo.getUserInfo().getUserName());

		final EditText edtPwd = (EditText) inputPwdDialog.getWindow().findViewById(R.id.dialog_continuebuy_et_fenShu);
		edtPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
		btnYes.setText("确定");
		btnYes.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (edtPwd.getText() != null && edtPwd.getText().toString() != "") {
					String strPwd = edtPwd.getText().toString();
					if (strPwd.length() != 32) {
						strPwd = MD5.md5(strPwd);
					}
					if (strPwd.equals(UserInfo.getUserInfo().getUserPwd())) {
						UserInfo.getUserInfo().setInputPasswordTime();
						EDebug.o(this, "" + UserInfo.getUserInfo().getInputPasswordTime());
						inputPwdDialog.cancel();
						Toast.makeText(context, "密码正确，请再次点击购买", 500).show();
					} else {
						Toast.makeText(context, "密码错误", 500).show();
						edtPwd.setText(null);
					}
				} else {
					Toast.makeText(context, "密码不能为空", 500).show();
				}
			}
		});
		Button btnNo = (Button) inputPwdDialog.getWindow().findViewById(R.id.dialog_continue_btn_no);
		btnNo.setText("取消");
		btnNo.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				inputPwdDialog.cancel();
			}
		});
		inputPwdDialog.show();
	}
}
